package com.xjl.javacore.code.collectionexample.lambda;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * lambda 表达式的
 * Created by xiaojinlu1990@163.com on 2020/1/1.
 */
public class LambdaTest {
    public static void main(String[] args) {

        List<User> usersList = initUser();
//        排序
//        setForfamliy(usersList);
//        分组
//        groupForSax(usersList);
        //过滤
        filterForEmployee(usersList);
        //求和
        sumForIntbogDecimal(usersList);
        //判断不为空
        isEmpty(usersList);
//        最值
        bestValue(usersList);

        //转Map
        changeMap(usersList);
        //排序
        sortForId(usersList);
        //去重
        distinctForId();
        //获取list对象的某个字段组装成新list
        getForclum(usersList);
        //  批量设置list列表字段为同一个值
        batchSettings(usersList);
    }

    private static void batchSettings(List<User> usersList) {
        usersList.stream().forEach(a -> a.setSex("0"));
    }

    private static void getForclum(List<User> usersList) {
        List<Long> userIdList = usersList.stream().map(a -> a.getId()).collect(Collectors.toList());
    }
    private static void distinctForId() {
        List<Long> idList = new ArrayList<Long>();
        idList.add(1L);
        idList.add(1L);
        idList.add(2L);
        List<Long> distinctIdList = idList.stream().distinct().collect(Collectors.toList());
    }

    private static void sortForId(List<User> usersList) {
        //单字段排序，根据id排序
        usersList.sort(Comparator.comparing(User::getId));
        //多字段排序，根据id，年龄排序
        usersList.sort(Comparator.comparing(User::getId).thenComparing(User::getAge));
    }

    /**
     * List -> Map
     * 需要注意的是：
     * toMap 如果集合对象有重复的key，会报错Duplicate key ....
     *  user1,user2的id都为1。
     *  可以用 (k1,k2)->k1 来设置，如果有重复的key,则保留key1,舍弃key2
     */

    private static void changeMap(List<User> usersList) {
        Map<Long, User> userMap = usersList.stream().collect(Collectors.toMap(User::getId, a -> a,(k1, k2)->k1));
    }

    private static void bestValue(List<User> usersList) {
        //最小
        Date minEntryDate = usersList.stream().map(User::getEntryDate).min(Date::compareTo).get();

        //最大
        Date maxEntryDate = usersList.stream().map(User::getEntryDate).max(Date::compareTo).get();
    }

    private static void isEmpty(List<User> usersList) {
        //        判断对象空
        usersList.stream().filter(x -> x!=null);
        usersList.stream().filter(Objects::nonNull);

//        判断字段空
        usersList.stream().filter(x -> x.getId()!=null);
    }

    private static void sumForIntbogDecimal(List<User> usersList) {
        //基本类型
        int sumAge = usersList.stream().mapToInt(User::getAge).sum();
        System.out.println(sumAge);
        //BigDecimal求和
        BigDecimal totalQuantity = usersList.stream().map(User::getFamilyMemberQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
        System.out.println(totalQuantity);
        BigDecimal totalQuantity2 = usersList.stream().map(User::getFamilyMemberQuantity).reduce(BigDecimal.ZERO, BigDecimalUtils::sum);
        System.out.println(totalQuantity2);

    }

    /**
      * 过滤
      * @Author xiaojinlu
      * @Description
      * @Date 2020/1/1 17:20
      * @Param
      * @return
      **/
    private static void filterForEmployee(List<User> usersList) {
        //排除掉工号为20200101的用户
        List<User> userCommonList = usersList.stream().filter(a -> !a.getJobNumber().equals("20200101")).collect(Collectors.toList());
    }

    /**
   * 分组
   * @Author xiaojinlu
   * @Description
   * @Date 2020/1/1 17:17
   * @Param
   * @return
   **/
    private static void groupForSax(List<User> usersList) {
        Map<String, List<User>> groupBySex = usersList.stream().collect(Collectors.groupingBy(User::getSex));
        for (Map.Entry<String, List<User>> entryUser : groupBySex.entrySet()) {
            String key = entryUser.getKey();
            List<User> entryusersList = entryUser.getValue();
        }
    }

    /**
     *  排序
     * @param usersList
     */
    private static void setForfamliy(List<User> usersList) {
        usersList.sort(Comparator.comparing(User::getFamilyMemberQuantity));
        for (int i = 0; i < usersList.size(); i++) {
            User user =  usersList.get(i);
            System.out.println(user.toString());
        }
    }

    /**
     * 初始化数据
     * @return
     */
    private static List<User> initUser() {
        List<User> usersList = new ArrayList<>();
        for (int i = 0; i < new Random().nextInt(100); i++) {
            User user = new User();
            user.setAge(new Random().nextInt(10));
            user.setSex(new Random().nextInt(2)+"");
            user.setId(new Random().nextLong());
            user.setFamilyMemberQuantity(new BigDecimal(Math.random() * 10044));
            //System.out.println(user.toString());
            usersList.add(user);
        }
        return usersList;
    }
}
